package com.hospital.yygh.hosp.controller.api;


import com.hospital.yygh.common.exception.HospitalException;
import com.hospital.yygh.common.helper.HttpRequestHelper;
import com.hospital.yygh.common.result.Result;
import com.hospital.yygh.common.result.ResultCodeEnum;
import com.hospital.yygh.common.utils.MD5;
import com.hospital.yygh.hosp.service.DepartmentService;
import com.hospital.yygh.hosp.service.HospitalService;
import com.hospital.yygh.hosp.service.HospitalSetService;
import com.hospital.yygh.hosp.service.ScheduleService;
import com.hospital.yygh.model.hosp.Department;
import com.hospital.yygh.model.hosp.Hospital;
import com.hospital.yygh.model.hosp.HospitalSet;
import com.hospital.yygh.model.hosp.Schedule;
import com.hospital.yygh.vo.hosp.DepartmentQueryVo;
import com.hospital.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 医院、科室
 */
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    /**
     * hospital-manage的网页点击事件生效后，先执行hospital-manage它的ApiController代码，在执行service层
     * hospital-manage的service层执行下面的HttpRequestHelper.sendRequest()代码
     * HttpRequestHelper.sendRequest()里面封装同步请求，就转到service-hosp的ApiController控制层代码
     * JSONObject respone = HttpRequestHelper.sendRequest(paramMap,this.getApiUrl()+"/api/hosp/schedule/list");
     * 封装了api的url和参数，再由url带参数发送请求带到了这里
     */

    private static final Logger log = LoggerFactory.getLogger(ApiController.class);

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    /**
     * 上传医院接口
     *
     * @param request
     * @return
     */
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        // 获取医院传递过来的信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 为了避免后面遍历，讲map中的String[]转换成Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        // 校验签名是否一致
        // 1.获取医院系统传递过来的签名
        String hospSign = (String) paramMap.get("sign");
        log.info("医院系统传递过来的签名hospSign: " + hospSign);
        // 2.根据传递过来的医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        log.info("数据库查询的签名signKey: " + signKey);
        // 3.把查询出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        log.info("加密signKey: " + signKeyMD5);
        // 4.判断签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        // 图片数据采取base64工具类传输，在传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String)paramMap.get("logoData");
        logoData = logoData.replaceAll(" ","+"); // “ ”中间有个空格
        paramMap.put("logoData",logoData);
        // 调用service的方法
        hospitalService.save(paramMap);
        return Result.ok();

    }

    /**
     * 查询医院接口
     *
     * @param request
     * @return
     */
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {

        // 获取医院传递过来的信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 为避免后面遍历，将map中的String[]转换成Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        // 校验签名是否一致
        // 1.获取医院系统传递过来的签名
        String hospSign = (String) paramMap.get("sign");
        log.info("医院系统传递过来的签名hospSign: " + hospSign);

        // 2.根据传递过来的医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        log.info("数据库查询的签名signKey: " + signKey);

        // 3.把查询出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        log.info("加密signKey: " + signKeyMD5);

        // 4.判断签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        // 调用service的方法
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);

    }

    /**
     * 上传科室的接口
     *
     * @param request
     * @return
     */
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        // 获取医院传递过来的信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 为避免后面遍历，将map中的String[]转换成Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        // 校验签名是否一致
        // 1.获取医院系统传递过来的签名
        String hospSign = (String) paramMap.get("sign");

        // 2.根据传递过来的医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        // 3.把查询出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);

        // 4.判断签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        // 调用service的方法
        departmentService.save(paramMap);
        return Result.ok();
    }

    /**
     * 科室查询接口
     *
     * @param request
     * @return
     */
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {
        // 获取医院传递过来的信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 为避免后面遍历，将map中的String[]转换成Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        // 校验签名是否一致
        // 1.获取医院系统传递过来的签名
        String hospSign = (String) paramMap.get("sign");
        // 当前页
        int page = Integer.parseInt((String) paramMap.get("page"));
        if (StringUtils.isEmpty(page)) {
            page = 1;
        }
        // 每页显示的记录数
        int limit = Integer.parseInt((String) paramMap.get("limit"));
        if (StringUtils.isEmpty(limit)) {
            limit = 1;
        }
        // 2.根据传递过来的医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 3.把查询出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        // 4.判断签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        // 调用service的方法，查询条件的值封装到departmentQueryVo中
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> departmentPage = departmentService.finPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(departmentPage);
    }

    /**
     * 删除科室接口
     *
     * @param request
     * @return
     */
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        // 获取医院传递过来的信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 为避免后面遍历，将map中的String[]转换成Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        // 1.获取医院编号和科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        // 校验
        String hospSign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 把查询出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        // 调用service的方法
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    /**
     * 上传排班
     *
     * @param request
     * @return
     */
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        // 获取传递过来的科室信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 为避免后面遍历，将map中的String[]转换成Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        // 校验
        String hospSign = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");
        // 加密后对比
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        // 调用service的方法
        scheduleService.save(paramMap);
        return Result.ok();
    }

    /**
     * 查询排班
     *
     * @param request
     * @return
     */
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        // 获取传递过来的科室信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 为避免后面遍历，将map中的String[]转换成Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        // 医院编号
        String hoscode = (String) paramMap.get("hoscode");
        // 科室编号
        String depcode = (String) paramMap.get("depcode");

        // 当前页
        int page = Integer.parseInt((String) paramMap.get("page"));
        if (StringUtils.isEmpty(page)) {
            page = 1;
        }
        // 每页显示的记录数
        int limit = Integer.parseInt((String) paramMap.get("limit"));
        if (StringUtils.isEmpty(limit)) {
            limit = 1;
        }
        // 校验
        String hospSign = (String) paramMap.get("sign");
        // 加密后对比
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        // 调用service查询，查询条件的值封装到departmentQueryVo中
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> schedulePage = scheduleService.finPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(schedulePage);
    }

    /**
     * 删除排班
     *
     * @param request
     * @return
     */
    @PostMapping("schedule/remove")
    public Result remove(HttpServletRequest request) {
        // 获取医院传递过来的信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 为避免后面遍历，将map中的String[]转换成Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        // 1.获取医院编号和科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        // 校验
        String hospSign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 把查询出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        // 调用service的方法
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

}
